package com.lzh.chatnote.user.service.adapter;

import cn.hutool.core.util.RandomUtil;
import com.lzh.chatnote.user.domain.entity.AuthData;
import com.lzh.chatnote.user.domain.entity.ItemConfig;
import com.lzh.chatnote.user.domain.entity.User;
import com.lzh.chatnote.user.domain.entity.UserBackpack;
import com.lzh.chatnote.user.domain.vo.response.user.BadgeResp;
import com.lzh.chatnote.webSocket.enums.LoginTypeEnum;
import lombok.Data;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

@Component
@Data
public class UserAdapter {


    /**
     * 填充用户信息
     * @param userInfo 授权的微信用户信息
     * @param user  用户信息
     * @return 用户信息实体
     */
    public static User fillUser(WxOAuth2UserInfo userInfo,User user) {
        User buidlUser = User.builder()
                .id(user.getId())
                .openId(user.getOpenId())
                .username(user.getOpenId())
                .password(RandomUtil.randomString(10))
                .name(userInfo.getNickname())
                .avatar(userInfo.getHeadImgUrl())
                .sex(userInfo.getSex())
                .build();
        if(buidlUser.getName().length() > 8){
            buidlUser.setName("名字过长"+ RandomUtil.randomString(100000));
        }
        return buidlUser;
    }


    /**
     * 填充第三方用户信息
     * @param authData  第三方授权信息
     * @return 用户信息实体
     */
    public static User fillThreeUserInfo(AuthData authData,String loginType) {
        User buidlUser = User.builder()
                .openId(authData.getUuid())
                .username(authData.getUuid())
                .password(RandomUtil.randomString(10))
                .name(authData.getNickname())
                .avatar(authData.getAvatar())
                .registerType(LoginTypeEnum.of(loginType).getValue())
                .sex("男".equals(authData.getGender()) ? 1 : 0 )
                .build();
        if(buidlUser.getName().length() > 8){
            buidlUser.setName("名字过长"+ RandomUtil.randomString(100000));
        }
        return buidlUser;
    }




    public static void main(String[] args) {
        System.out.println(RandomUtil.randomString(10));
    }

    public List<BadgeResp> buildWearingBadge(List<ItemConfig> allBadgeItems, List<UserBackpack> containBadges, User user) {
        if(Objects.isNull(user)){
            return Collections.emptyList();
        }

        Set<Long> containIds = containBadges.stream().map(UserBackpack::getItemId).collect(Collectors.toSet());
        return allBadgeItems.stream().map(itemConfig -> {
            BadgeResp badgeResp = new BadgeResp();
                    BeanUtils.copyProperties(itemConfig,badgeResp);
                    badgeResp.setObtain(containIds.contains(itemConfig.getId()) ? 1 : 0);
                    if(Objects.equals(itemConfig.getId(),user.getItemId())){
                        badgeResp.setWearing(1);
                    }else{
                        badgeResp.setWearing(0);
                    }
                    return badgeResp;
                })
                .sorted(
                //先根据是否拥有排序
                        Comparator.comparing(BadgeResp::getObtain,Comparator.reverseOrder())
                //在根据是否佩戴排序
                        .thenComparing(BadgeResp::getWearing,Comparator.reverseOrder()))
                .collect(Collectors.toList());
    }
}
